package com.dc.portal.controller;

import com.dc.base.BaseResponse;
import com.dc.member.constants.WebConstants;
import com.dc.member.feign.IMemberServiceFeignPortal;
import com.dc.member.output.dto.UserOutputDTO;
import com.dc.web.base.BaseWebController;
import com.dc.web.utils.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: KEYD
 * @Date: 2019/12/29 11:13
 * @Desc:
 */
@Controller  //使用freemarker模板就不能使用RestController
public class IndexController extends BaseWebController {

    private static final String INDEX_FTL = "index";  // index 页面

    @Autowired
    private IMemberServiceFeignPortal iMemberServiceFeignPortal;

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
        // 1.从cookie 中 获取 会员token
        String token = CookieUtils.getCookieValue(request, WebConstants.LOGIN_TOKEN_COOKIENAME, true);
        if (!StringUtils.isEmpty(token)) {
            // 2.调用会员服务接口,查询会员用户信息
            BaseResponse<UserOutputDTO> userInfo = iMemberServiceFeignPortal.getInfo(token);
            if (isSuccess(userInfo)) {
                UserOutputDTO data = userInfo.getData();
                if (data != null) {
                    String mobile = data.getMobile();
                    // 对手机号码实现脱敏
                    String desensMobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                    model.addAttribute("desensMobile", desensMobile);
                }
            }
        }
        return INDEX_FTL;
    }
}
/**
 * 1、完成退出功能，
 * 2、实现唯一登录心跳检测：前端定时器。使用js
 */
